package com.example.xianji.activity;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Main_JunMing_DetailsActivity extends Activity implements OnClickListener {
    private RelativeLayout junming_detail_back;
    private Button junming_ronghe_zixun;
    private TextView junming_details_title, junming_details_name;
    private WebView junming_details_webview;
//    private SharedPreferences sp;
//    private Editor ed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__jun_ming__details);
        //XJApplication.getInstance().addActivity(this);
        junming_detail_back = (RelativeLayout) findViewById(R.id.junming_detail_relative_back);
        junming_ronghe_zixun = (Button) findViewById(R.id.junming_ronghe_zixun);
        junming_details_title = (TextView) findViewById(R.id.junming_details_title);
        junming_details_name = (TextView) findViewById(R.id.junming_details_name);
        junming_details_name.setText(getIntent().getStringExtra("junming_parent_name"));
        junming_details_webview = (WebView) findViewById(R.id.junming_details_webview);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        junming_details();
        junming_detail_back.setOnClickListener(this);
        junming_ronghe_zixun.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.junming_detail_relative_back) {
            this.finish();
        } else if (v.getId() == R.id.junming_ronghe_zixun) {
            if (PreferenceEngine.getInstance().getBoolean(Constant.SP.NAME, Constant.SP.KEY_XIANJI_DENGLU, false)) {
//            if (sp.getBoolean("xianji_denglu", false)) {
                Intent intent = new Intent(Main_JunMing_DetailsActivity.this, Main_Want_ZiXunActivity.class);
                intent.putExtra("junming_id", getIntent().getIntExtra("junming_id", 0));
                intent.putExtra("from_what", 1);
                startActivity(intent);
            } else {
                startActivity(new Intent(Main_JunMing_DetailsActivity.this, Main_LoginActivity.class));
            }
        }
    }

    public void junming_details() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("convert_id", getIntent().getIntExtra("junming_id", 0) + "");
        httpUtils.send(HttpMethod.POST, Contant.junming_details, parms, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                try {
                    JSONObject object = new JSONObject(arg0.result);
                    if (object.getInt("status") == 1) {
                        JSONObject obj = object.getJSONObject("data");
                        junming_details_title.setText(obj.getString("title"));
                        junming_details_webview.loadDataWithBaseURL(null, obj.getString("details"), "text/html", "utf-8", null);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}